﻿/*************************************************************************************
 * 文 件 名:   HK_AutoGenerateTemplate
 * 
 * 描    述:   自动生成项目脚本模板的工具。（若Assets根目录下有 _Scripts 文件夹，则取消操作）
               自动生成 _Scripts→Commands、Events、Models、Systems、ViewControllers 文件夹结构；
               自动生成 “项目名”+“_Architecture”框架脚本；
               自动生成 “Controller”视图层模板脚本；
               自动生成 “IRuntimeModel”模型层模板脚本；
               自动生成 “Events_Global”空脚本；
               自动生成 “IAudioSystem”音频管理系统层脚本;
 *
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2023/11/17
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.IO;
using UnityEditor;
using UnityEngine;

public class HK_AutoGenerateTemplate : EditorWindow
{
    const int AutoGenerateTemplatePriority = 28;

    string architectureCode;
    string controllerCode;
    string runtimeModelCode;
    string audioSystemCode;

    [MenuItem("HKTools/Auto Generate Template(自动化生成项目模板)", priority = AutoGenerateTemplatePriority)]
    static void Init()
    {
        HK_AutoGenerateTemplate generateTemplate = GetWindow<HK_AutoGenerateTemplate>(false, "Auto Generate Template", true);

        generateTemplate.Show();
    }

    void OnGUI()
    {
        GUILayout.Label("注意事项：\n1. 本工具可自动生成项目脚本模板。\n2. 点击“项目初始化”按钮，生成_Scripts文件夹及模板脚本结构。\n3. 若场景中存在_Scripts文件夹则取消生成操作。");

        GUILayout.Space(10);

        if (GUILayout.Button("项目初始化", GUILayout.Width(200)))
        {
            AutoGenerateTemplate();
        }
    }

    void AutoGenerateTemplate()
    {
        GenerateScriptsFolder();
        AssetDatabase.Refresh();
    }

    void GenerateScriptsFolder()
    {
        bool isHaveScripts = (AssetDatabase.IsValidFolder("Assets/_Scripts") || AssetDatabase.IsValidFolder("Assets/_Script"));

        if (isHaveScripts)
        {
            Debug.LogWarning("_Scripts文件夹已存在");
            return;
        }

        string productName = Application.productName;

        if (productName.Contains(" "))
        {
            productName = productName.Replace(" ", "");
        }

        Debug.Log(productName);

        architectureCode =
        "using QFramework;" +
        "\r\n" +
        "\r\npublic class " + productName + "_Architecture : Architecture<" + productName + "_Architecture>" +
        "\r\n{" +
        "\r\n    protected override void Init()" +
        "\r\n    {" +
        "\r\n        this.RegisterModel<IRuntimeModel>(new RuntimeModel());" +
        "\r\n        this.RegisterSystem<IAudioSystem>(new AudioSystem());" +
        "\r\n    }" +
        "\r\n}";

        controllerCode =
        "using UnityEngine;" +
        "\r\nusing QFramework;" +
        "\r\n" +
        "\r\npublic class Controller : MonoBehaviour, IController" +
        "\r\n{" +
        "\r\n    public IArchitecture GetArchitecture()" +
        "\r\n    {" +
        "\r\n        return " + productName + "_Architecture.Interface;" +
        "\r\n    }" +
        "\r\n}";

        runtimeModelCode =
        "using QFramework;" +
        "\r\n" +
        "\r\n/// <summary>" +
        "\r\n/// 运行数据 模型层" +
        "\r\n/// </summary>" +
        "\r\npublic interface IRuntimeModel : IModel" +
        "\r\n{" +
        "\r\n" +
        "\r\n}" +
        "\r\n" +
        "\r\npublic class RuntimeModel : AbstractModel, IRuntimeModel" +
        "\r\n{" +
        "\r\n    protected override void OnInit()" +
        "\r\n    {" +
        "\r\n" +
        "\r\n    }" +
        "\r\n}";

        audioSystemCode =
        "using QFramework;" +
        "\r\nusing UnityEngine.UI;" +
        "\r\n" +
        "\r\n/// <summary>" +
        "\r\n/// 音频管理 系统层" +
        "\r\n/// </summary>" +
        "\r\npublic interface IAudioSystem : ISystem" +
        "\r\n{" +
        "\r\n" +
        "\r\n}" +
        "\r\n" +
        "\r\npublic class AudioSystem : AbstractSystem, IAudioSystem" +
        "\r\n{" +
        "\r\n    protected override void OnInit()" +
        "\r\n    {" +
        "\r\n        HK_Button.defaultAudio_Event += OnClickHKButton;" +
        "\r\n        HK_Toggle.defaultAudio_Event += OnClickHKToggle;" +
        "\r\n    }" +
        "\r\n" +
        "\r\n    void OnClickHKButton()" +
        "\r\n    {" +
        "\r\n" +
        "\r\n    }" +
        "\r\n" +
        "\r\n    void OnClickHKToggle()" +
        "\r\n    {" +
        "\r\n" +
        "\r\n    }" +
        "\r\n}";

        AssetDatabase.CreateFolder("Assets", "_Scripts");
        AssetDatabase.CreateFolder("Assets/_Scripts", "Commands");
        AssetDatabase.CreateFolder("Assets/_Scripts", "Events");
        AssetDatabase.CreateFolder("Assets/_Scripts", "Models");
        AssetDatabase.CreateFolder("Assets/_Scripts", "Systems");
        AssetDatabase.CreateFolder("Assets/_Scripts", "ViewControllers");

        File.WriteAllText("Assets/_Scripts/" + productName + "_Architecture.cs", architectureCode);
        File.WriteAllText("Assets/_Scripts/Controller.cs", controllerCode);
        File.WriteAllText("Assets/_Scripts/Models/IRuntimeModel.cs", runtimeModelCode);
        File.WriteAllText("Assets/_Scripts/Events/Events_Global.cs", "");
        File.WriteAllText("Assets/_Scripts/Systems/IAudioSystem.cs", audioSystemCode);
    }
}
